/*
 * Classname IBrokerManagerTest.java
 * 
 * Version Information
 * Date Feb 23, 2006
 *
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.isso.hibernate.tests;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import net.amsoft.iservice.isso.hibernate.IBroker;
import net.amsoft.iservice.isso.hibernate.Principal;
import net.amsoft.iservice.isso.hibernate.PrincipalMetaData;
import net.amsoft.iservice.isso.hibernate.Synonym;
import net.amsoft.iservice.isso.util.ISSOConstants;
import net.amsoft.iservice.isso.util.ISSOHelpers;
import net.amsoft.iservice.isso.util.Messages;
import net.amsoft.iservice.util.exception.IServiceException;

/**
 * @author jyoti
 *  
 */
public class PrincipalManagerTest extends TestCase
{

    public static void main(String[] oArgs) {
        TestRunner.run(suite());
    } // main()

    public static Test suite() {
        return new TestSuite(PrincipalManagerTest.class);

    } // suite()

    protected void setUp() throws Exception {

        super.setUp();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    private static void debug() {
        System.out.println("==================  DEBUG  ===================");
        System.out.println("####  IBROKER TABLE  ####");
        Principal.listAll();

        System.out.println("==================  DEBUG  ===================");
    }

    private void setDB() throws SQLException {
        Connection oCon = null;
        try {
            oCon = getConnection();
            String strDeleteIBroker = "DELETE FROM IBROKER WHERE SERVER_NAME='"
                    + Messages.getString("ibroker.IBrokerName") + "'";
            PreparedStatement psDelete = oCon
                    .prepareStatement(strDeleteIBroker);
            //psDelete.execute();
            oCon.commit();
        } catch (Exception e) {
        } finally {
            oCon.close();
        }

    }

    public Connection getConnection() {
        Connection oCon = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String DB_URL = "jdbc:mysql://localhost:3306/hibernate";
            oCon = DriverManager.getConnection(DB_URL, "root", "root");
            oCon.setAutoCommit(false);
        } catch (Exception e) {
            System.out.println("Exception :" + e.getMessage());
        }
        return oCon;
    }
    private void setDBFromGet() throws SQLException {
        Connection oCon = null;
        try {
            oCon = getConnection();
            String strDeleteIBroker = "DELETE FROM IBROKER WHERE SERVER_NAME='"
                    + Messages.getString("ibroker.IBrokerName") + "'";
            PreparedStatement psDelete = oCon
                    .prepareStatement(strDeleteIBroker);
            psDelete.execute();
            oCon.commit();
        } catch (Exception e) {
        }

        try {
            oCon = getConnection();
            String strCreateIBroker = "INSERT INTO IBROKER (SERVER_NAME , STATE_ID )VALUES ('"
                    + Messages.getString("ibroker.IBrokerName") + "' , 1";
            PreparedStatement psCreate = oCon
                    .prepareStatement(strCreateIBroker);
            psCreate.execute();
            oCon.commit();
        } catch (Exception e) {
        }

        finally {
            oCon.close();
        }

    }

    private ResultSet getDataFromDB(String sql, Connection oCon) {
        String strSQLGet = sql;

        try {
            PreparedStatement psGet = oCon.prepareStatement(strSQLGet);
            ResultSet oRs = psGet.executeQuery();
            return oRs;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;

    }

    public void testCreate() throws IServiceException, SQLException {

        IBrokerTest iBT = new IBrokerTest();
        IBrokerTest.mB();

        int bId = 1;
        Principal oPrincipal = new Principal();
        if (bId > 0) {
            IBroker oIB = (IBroker) ISSOHelpers.fetch(IBroker.class, new Long(
                    bId));
            oPrincipal.setBroker(oIB);
        }
        oPrincipal.setState((short) ISSOConstants.STATE_ACTIVE);

        oPrincipal.insert();

        Connection oCon = getConnection();

        String sqlGet = "SELECT SYNONYM FROM SYNONYM  WHERE PRINCIPAL_ID =1";
        System.out.println(sqlGet);
        ResultSet rs = getDataFromDB(sqlGet, oCon);

        rs.next();
        //assertEquals(rs.getString(1),
        // Messages.getString("ibroker.IBrokerName"));
        oCon.close();

        debug();

    }

    public void testMakeGINOSynonym() throws SQLException {

        int sId = 1;
        int gIN = 0;
        String name = Messages.getString("principal.globalINumber");

        Synonym oS = new Synonym();
        oS.setSynonym(name);
        oS.setState((short) ISSOConstants.STATE_ACTIVE);
        if (sId > 0) {
            Principal oIB = (Principal) ISSOHelpers.fetch(Principal.class,
                    new Long(sId));
            oS.setPrincipal(oIB);

        }
        if (gIN > 0) {
            Synonym oGINSyn = (Synonym) ISSOHelpers.fetchSynonym(Synonym.class,
                    new Long(gIN));
            oS.setGlobalINumber(oGINSyn);
        }

        try {
            oS.insert();
        } catch (IServiceException e) {

            e.printStackTrace();
        }

        Connection oCon = getConnection();
        String sqlGet = "SELECT SYNONYM FROM SYNONYM WHERE "
                + "PRINCIPAL_ID = 1 "; /*
                                        * ;( SELECT PRINCIPAL_ID FROM SYNONYM " +
                                        * "WHERE GLOBAL_INUMBER='" +
                                        * Messages.getString("principal.globalINumber") +
                                        * "')";
                                        */
        System.out.println(sqlGet);
        ResultSet rs = getDataFromDB(sqlGet, oCon);
        rs.next();

        //        assertEquals(rs.getString(1), Messages
        //                .getString("principal.globalINumber"));
        rs.close();
        rs = null;
        debug();
    }

    public void testMakeSynonym() throws SQLException {

        int sId = 1;
        int gIN = 1;
        String name = "=foo";

        Synonym oS = new Synonym();
        oS.setSynonym(name);
        oS.setState((short) ISSOConstants.STATE_ACTIVE);
        if (sId > 0) {
            Principal oIB = (Principal) ISSOHelpers.fetch(Principal.class,
                    new Long(sId));
            oS.setPrincipal(oIB);

        }
        if (gIN > 0) {
            Synonym oGINSyn = (Synonym) ISSOHelpers.fetchSynonym(Synonym.class,
                    new Long(gIN));
            oS.setGlobalINumber(oGINSyn);
        }

        try {
            oS.insert();
        } catch (IServiceException e) {

            e.printStackTrace();
        }
        debug();
        Connection oCon = getConnection();
        String sqlGet = "SELECT SYNONYM FROM SYNONYM WHERE "
                + "PRINCIPAL_ID = 1 AND GLOBAL_INUMBER=1 ";
        /* + Messages.getString("principal.globalINumber") + "')"; */
        System.out.println(sqlGet);
        ResultSet rs = getDataFromDB(sqlGet, oCon);
        rs.next();

        assertEquals(rs.getString(1), "=foo");
        rs.close();
        rs = null;
        //        debug();

    }

    public void testPrincipalMetaData() throws SQLException {
        int sId = 1;
        PrincipalMetaData oPMD = new PrincipalMetaData();
        oPMD.setChallengeQuestionId(1);
        if (sId > 0) {
            Principal oIB = (Principal) ISSOHelpers.fetch(Principal.class,
                    new Long(sId));
            oPMD.setPrincipal(oIB);
        }
        oPMD.setEncActivationToken(Messages
                .getString("principal.activationToken"));
        oPMD.insert();
        debug();
        Connection oCon = getConnection();
        String sqlGet = "SELECT ENC_ACTIVATION_TOKEN FROM PRINCIPAL_METADATA WHERE "
                + "PRINCIPAL_ID = 1 ";
        /* + Messages.getString("principal.globalINumber") + "')"; */
        System.out.println(sqlGet);
        ResultSet rs = getDataFromDB(sqlGet, oCon);
        rs.next();

        assertEquals(rs.getString(1), "sActivationToken");
        rs.close();
        rs = null;
        //        debug();

    }

    public void testUpdatePrincipalData() throws IServiceException,
            SQLException {
        PrincipalMetaData oData = (PrincipalMetaData) ISSOHelpers
                .fetchMetaData(new Long(1));
        oData.setChallengeQuestionId(5);
        oData.setEmail("something@something.com");
        oData.setEncGreeting("greeting");
        oData.update();

        debug();
        Connection oCon = getConnection();
        String sqlGet = "SELECT ENC_PERSONALIZED_GREETING , CHALLENGE_QUESTION_ID FROM PRINCIPAL_METADATA WHERE "
                + "PRINCIPAL_ID = 1 ";
        /* + Messages.getString("principal.globalINumber") + "')"; */
        System.out.println(sqlGet);
        ResultSet rs = getDataFromDB(sqlGet, oCon);
        rs.next();

        assertEquals(rs.getString(1), "greeting");
        assertEquals(rs.getInt(2), 5);

        rs.close();
        rs = null;
    }

    public void testGetPrincipal() {
        Principal oPrincipal = (Principal) ISSOHelpers.fetch(Principal.class,
                new Long(1));
        assertEquals(new Integer(1), oPrincipal.getBroker().getId());
    }
    public void testRemoveSynonym() throws IServiceException, SQLException {
        int sId = 1;
        int gIN = 1;
        String name = "=foo";

        Synonym oS = ISSOHelpers.fetchSynonym(name);
        Principal oPrincipal = (Principal) ISSOHelpers.fetch(Principal.class,
                new Long(1));
        oPrincipal.removeSynonym(oS);

        debug();
        Connection oCon = getConnection();
        String sqlGet = "SELECT STATE_ID FROM SYNONYM WHERE "
                + "PRINCIPAL_ID = 1 AND GLOBAL_INUMBER=1 ";
        /* + Messages.getString("principal.globalINumber") + "')"; */
        System.out.println(sqlGet);
        ResultSet rs = getDataFromDB(sqlGet, oCon);
        rs.next();

        assertEquals(rs.getShort(1), 3);
        rs.close();
        rs = null;
    }
}